//
//  DIYViewController.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "DIYViewController.h"
#import "MenuControlView.h"
#import "WWCollectCollectionViewCell.h"
#import "WHNHistoryCollectionViewCell.h"
#import "DownCollectionViewCell.h"
#import "PlayHistory.h"
#import "History.h"
#import "FPlayViewController.h"
#import "WHNThirdViewController.h"
#import "AlbumFViewController.h"
#import "Album.h"
#import "WWContentTableCell.h"

@interface DIYViewController () <MenuControlViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, HisrotyDelegate, AlbumeHistoryDelegate>

@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)MenuControlView *menuView;

@end

static NSString *systemCell = @"systemCell";
static NSString *downCell = @"down";
@implementation DIYViewController

#pragma mark - 懒加载
#pragma mark 初始化collectionView
-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.itemSize = CGSizeMake(GViewWidth(self.view), GViewHeight(self.view) - 64 - 49 - FitConstant_YorHeight(50));
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, FitConstant_YorHeight(50), self.view.frame.size.width, GViewHeight(self.view) - 64 - 49 - FitConstant_YorHeight(50)) collectionViewLayout:flowLayout];
        _collectionView.pagingEnabled = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:systemCell];
        [_collectionView registerClass:[WWCollectCollectionViewCell class] forCellWithReuseIdentifier:@"collectCell"];
        [_collectionView registerClass:[WHNHistoryCollectionViewCell class] forCellWithReuseIdentifier:@"history"];
        [_collectionView registerClass:[DownCollectionViewCell class] forCellWithReuseIdentifier:downCell];
    }
    return _collectionView;
}

#pragma mark 初始化menuView
-(MenuControlView *)menuView
{
    if (!_menuView) {
        _menuView = [[MenuControlView alloc] initWithFrame:FitRectMake(0, 0, 375, 50)];
        _menuView.delegate = self;
        _menuView.numberOfMenu = 2;
        _menuView.dataSource = @[@"收藏", @"历史"];
    }
    return _menuView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"定制听";
    self.collectionView.bounces = NO;
    [self.view addSubview:self.menuView];
    [self.view addSubview:self.collectionView];
}

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}


#pragma mark - collectionView代理方法

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.row == 0) {
//        DownCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:downCell forIndexPath:indexPath];
//        return cell;
//    }
    if (indexPath.row == 0) {
        WWCollectCollectionViewCell *wwCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectCell" forIndexPath:indexPath];
        wwCell.delegate = self;
        return wwCell;
    }
    
    if (indexPath.row == 1) {
        WHNHistoryCollectionViewCell *WHNcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"history" forIndexPath:indexPath];
        WHNcell.delegate = self;
        return WHNcell;
    }
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:systemCell forIndexPath:indexPath];
    
    [cell NightWithType:UIViewColorTypeNormal];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        WWCollectCollectionViewCell *wwCell = (WWCollectCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        [wwCell selectAction];
    }
    
    if (indexPath.row == 1) {
        WHNHistoryCollectionViewCell *WHNcell = (WHNHistoryCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        [WHNcell selectAction];
        [WHNcell playHistory];
    }
}

#pragma mark - scrollView代理方法, 用来控制menuView的切换
#pragma mark 结束减速
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = (NSInteger)(scrollView.contentOffset.x + 50) / scrollView.frame.size.width;
    [_menuView selectCell:page];
}

#pragma mark - MenuViewDelegate
-(void)didSelectedMenu:(NSInteger)number
{
    self.collectionView.contentOffset = CGPointMake(self.collectionView.frame.size.width * number, 0);
}

#pragma mark HisrotyDelegate
-(void)didSelect:(id)history Section:(NSInteger)section
{
    if (section == 0) {
        FPlayViewController *vc = [[FPlayViewController alloc] init];
        PlayHistory *model = (PlayHistory *)history;
        vc.trackID = model.trackID;
        vc.trackUid = model.trackUid;
        vc.albumId = model.albumId;
        self.hidesBottomBarWhenPushed = YES;
        vc.tabBarController.tabBar.hidden = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else{
        WHNThirdViewController *vc = [[WHNThirdViewController alloc] init];
        History *model = (History *)history;
        vc.WHNPic = model.imageUrl;
        vc.WHNCount = model.acount;
        vc.RadioId = model.radioId;
        vc.Rname = model.rname;
        self.hidesBottomBarWhenPushed = YES;
        vc.tabBarController.tabBar.hidden = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark albumDelegate
-(void)didSelect:(id)history Secion:(NSInteger)section Kind:(BOOL)kind
{
    if (kind == 1 && section == 0) {
        AlbumFViewController *vc = [[AlbumFViewController alloc] init];
        Album *model = history;
        vc.trackID = model.trackId;
        vc.albumId = model.albumId;
        vc.alumbTitel = model.title;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else{
        AlbumFViewController *vc = [[AlbumFViewController alloc] init];
        WWTableContentModel *model = history;
        vc.trackID = [NSString stringWithFormat:@"%@", model.tracks];
        vc.albumId = [NSString stringWithFormat:@"%@", model.albumId];
        vc.alumbTitel = model.title;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
